package com.mg.swagger.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mg.swagger.framework.json.MgResponseJson;
import com.mg.swagger.framework.service.MgStorageService;

@RestController
@RequestMapping("/swagger-mg-ui/storage")
public class StorageController {
	
	private static final String AUTOWIRED_ERROR = "暂未配置MgStorageService的实现类";
	
	@Autowired(required = false)
	private MgStorageService storageService;

	@PostMapping(value = "/data")
	public MgResponseJson setData(String key, String value) {
		if (storageService == null) {
			return MgResponseJson.warn(AUTOWIRED_ERROR);
		}
		if (key == null || value == null) {
			return MgResponseJson.warn("参数名或值不能为空");
		}
		storageService.put(key, value);
		return MgResponseJson.ok();
	}

	@GetMapping(value = "/data")
	public MgResponseJson getData(String key) {
		if (storageService == null) {
			return MgResponseJson.warn(AUTOWIRED_ERROR);
		}
		String value = storageService.get(key);
		value = (value == null) ? "" : value;
		return MgResponseJson.ok(value);
	}
}